#!/bin/bash

# 此脚本根据当前目录和输入的一个参数(git_branch), 自动把git_repo放到上级目录, 然后同步到当前文件夹.

br='master'
sync_dir=''

if [ $# -lt 2 ];then
    echo "input two params: git_branch and your jenkins_job_dir"
    exit 1
else
    br=$1
    sync_dir=$2
    if [ ! -d ${sync_dir} ];then
        echo "not found dir <${sync_dir}>, exit"
        exit 1
    fi
fi

repo_dir="repo_usk_api"
usk_repo="git@gitlab-iot.yzs.io:testteam/usk-api-scripts.git"


echo "Git_Branch: ${br}"
echo "Sync_Dir  : ${sync_dir}"

function pull_repo()
{
    if [ ! -d ${repo_dir} ]; then
        git clone -b ${br} --depth 1  ${usk_repo} ${repo_dir}
    else
        cd ${repo_dir}
        git checkout .
        git pull origin $br
        cd -
    fi
}


function sync_files()
{
    echo $PWD
    \cp -fr ${repo_dir}/* $1/
}

## main ##
cd ..
pull_repo
sync_files "${sync_dir}"
cd -
